/*
 * Copyright 2009, Qys Creations 
 * 
 *  This file is part of Chameleon DB.
 *
 *  Chameleon DB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chameleon DB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Chameleon DB.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * */



public class db_thread extends Thread{
	db_server oServer = null;
	db_session oSession = null;

	db_thread(db_server oDatabaseServer) {
		
		oServer = oDatabaseServer;
	}

	public void run() {
		// Create possible types of query statments
		
		db_interface_statement[] aStatements = new db_interface_statement[9];
		aStatements[0] = new db_statement_create_database();
		aStatements[1] = new db_statement_create_table();
		aStatements[2] = new db_statement_create_index();
		aStatements[3] = new db_statement_select_from();
		aStatements[4] = new db_statement_insert_into();
		aStatements[5] = new db_statement_show_databases();
		aStatements[6] = new db_statement_show_tables();
		aStatements[7] = new db_statement_delete_from();
		aStatements[8] = new db_statement_update();
		
		try {
			
			while (true) {
				// Use global server object to get a new client connection
				oSession = oServer.get_query();
				db_query_handler oHandler = new db_query_handler(oSession.oSocket);
				String sSQL = "";
				
				// Negotiate with the client
				if (oHandler.helo()) {
				
					// Keep serving the client until it disconnects
					while (oHandler.get_next_query()) {

						// Could be optimized?
						sSQL = oHandler.get_SQL();
						
						// Prepare the result object and parse result object
						db_interface_result oDataSet = null;
						db_interface_parse_result oRes = null;

						for (int i = 0; i < aStatements.length; i++) {
							// Lets try to parse the query string
							oRes = aStatements[i].parse(sSQL);
							//System.out.println("statement " + i + " returned: " + oRes.get_result());
							if (oRes.get_result() == 0) {
								// The query string parses succesfully so run the query
								
								
								oDataSet = oRes.run_query(oServer, oSession);
								break;
							} else if (oRes.get_result() == -1) {
								// Statement matches the first 1 or 2 SQL keyword like
								// INSERT INTO, but fails later for some reason
								oDataSet = new db_result_error(db_util.DB_ERR_BAD_SQL_SYNTAX, "Bad SQL syntax");
								break;
							}
						}
						if (oDataSet == null) {
							// Unknown keywords starts query string like 
							// volapycsqlstatementgarbagefoo
							oDataSet = new db_result_error(db_util.DB_ERR_UNKNOWN_KEYWORDS, "Unknown SQL keyword: " + sSQL.split(" ")[0]);
						}
						// Send the response to the client
						oHandler.add_response(oDataSet.getBytes());
					}
				}
			}
		} catch (Exception e) {e.printStackTrace();}
	}
}
